package com.restaurant.application.genshanguser;

import com.google.common.base.Charsets;
import com.nebutown.commonunits.simpletreemenu.SimpleTreeMenu;
import com.nebutown.commonunits.simpletreemenu.support.MenuTree;
import com.nebutown.commonunits.simpleworkstationdesktop.SimpleWorkStationDesktop;
import com.nebutown.commonunits.simpleworkstationdesktop.support.ConfigData;
import com.nebutown.generation.Convert;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.root.RootImpl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

class UserDesktop {

    private static MenuTree PROTO_MENU = null;

    static {
        InputStream input = RootImpl.class.getResourceAsStream("menu.json");
        PROTO_MENU = Convert.fromJson(MenuTree.class, byteArrayToString(input));
    }

    public static UID buildWide(GenShangUserImpl impl) {
        SimpleWorkStationDesktop desktop;
        SimpleTreeMenu menu;
        if (Objects.nonNull(impl.getData().getWideDesktop())) {
            desktop = impl.getUnit(SimpleWorkStationDesktop.class, impl.getData().getWideDesktop());
            menu = impl.getUnit(SimpleTreeMenu.class, desktop.getMenuProvider());
        } else {
            desktop = impl.createNode(SimpleWorkStationDesktop.class);
            menu = impl.createNode(SimpleTreeMenu.class);
        }
        menu.init(PROTO_MENU.duplicate());
        desktop.init(new ConfigData()
                .withLeftPercent(20)
                .withTopPercent(0)
                .withRightPercent(0)
                .withLeft(menu.getId())
                .withAsTop(true)
        );

        impl.getData().withWideDesktop(desktop.getId());
        return desktop.getId();
    }

//    public static UID buildWide(UserImpl user) {
//        SimpleWorkStationDesktop menuDesktop;
//        SimpleTreeMenu menu;
//        if (user.getData().getWideDesktop() != null) {
//            menuDesktop = user.getUnit(SimpleWorkStationDesktop.class, user.getData().getWideDesktop());
//            menu = user.getUnit(SimpleTreeMenu.class, menuDesktop.getMenuProvider());
//        } else {
//            menuDesktop = user.createNode(SimpleWorkStationDesktop.class);
//            menu = user.createNode(SimpleTreeMenu.class);
//            user.getData().setWideDesktop(menuDesktop.getId());
//        }
//        menu.init(PROTO_MENU.duplicate());
//
//        menuDesktop.init(new ConfigData()
//                .withLeftPercent(20)
//                .withTopPercent(0)
//                .withRightPercent(0)
//                .withLeft(menu.getId())
//                .withAsTop(true)
//        );
//
//        user.getData().withWideDesktop(menuDesktop.getId());
//        return menuDesktop.getId();
//    }

    public static String byteArrayToString(InputStream inputStream) {

        try {
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            return new String(bytes, Charsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }

        throw new IllegalStateException("--- Can't read inputStream, make sure");
    }
}
